package com.yc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yc.R_Code;
import com.yc.Result;
import com.yc.entities.BloodSugarEquipment;
import com.yc.entities.custom.CustomEquipmentQueryRequestBean;
import com.yc.entities.custom.CustomEquipmentUpdateRequestBean;
import com.yc.service.BloodSugarEquipmentService;
import com.yc.service.BloodSugarPatientInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 设备 前端控制器
 * </p>
 *
 * @author kuakha
 * @since 2021-04-07
 */
@RestController
@Controller
@Slf4j
@RequestMapping("/bloodSugarEquipment")
@Api(value = "BloodSugarEquipmentController", tags = {"设备信息管理和条形码管理"})
public class BloodSugarEquipmentController {

    @Autowired
    private BloodSugarEquipmentService bloodSugarEquipmentService;

    @Autowired
    private BloodSugarPatientInfoService bloodSugarPatientInfoService;

    @ApiOperation(value = "pc查询设备信息")
    @PreAuthorize("hasAnyAuthority('sys:equipmentManage','sys:noAccess')")
    @PostMapping("getEquipmentInfo")
    public Result getEquipmentInfo(@Valid @RequestBody CustomEquipmentQueryRequestBean customEquipmentQueryRequestBean) {
        return bloodSugarEquipmentService.getAllEquipmentInfo(customEquipmentQueryRequestBean);
    }

    @ApiOperation(value = "pc修改设备信息")
    @PreAuthorize("hasAnyAuthority('sys:equipmentManage','sys:noAccess')")
    @PostMapping("updateEquipmentInfo")
    public Result updateEquipmentInfo(@Valid @RequestBody CustomEquipmentUpdateRequestBean customEquipmentUpdateRequestBean) {
        LambdaQueryWrapper<BloodSugarEquipment> equipmentLambdaQueryWrapper = Wrappers.lambdaQuery();
        equipmentLambdaQueryWrapper.ne(BloodSugarEquipment::getId, customEquipmentUpdateRequestBean.getId())
                .eq(BloodSugarEquipment::getDepartmentId, customEquipmentUpdateRequestBean.getDepartmentId())
                .eq(BloodSugarEquipment::getEquipmentCode, customEquipmentUpdateRequestBean.getEquipmentCode());
        if (bloodSugarEquipmentService.list(equipmentLambdaQueryWrapper).size() > 0) {
            return Result.error().message("该科室下已经存在该仪器编号");
        }
        LambdaQueryWrapper<BloodSugarEquipment> equipmentLambdaQueryWrapper1 = Wrappers.lambdaQuery();
        equipmentLambdaQueryWrapper1.eq(BloodSugarEquipment::getMac, customEquipmentUpdateRequestBean.getMac());
        if (bloodSugarEquipmentService.list(equipmentLambdaQueryWrapper1).size() > 0) {
            return Result.error().message("已存在该mac地址");
        }
        LambdaUpdateWrapper<BloodSugarEquipment> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(BloodSugarEquipment::getId, customEquipmentUpdateRequestBean.getId());
        if (StringUtils.isNotBlank(customEquipmentUpdateRequestBean.getEquipmentCode())) {
            lambdaUpdateWrapper.set(BloodSugarEquipment::getEquipmentCode, customEquipmentUpdateRequestBean.getEquipmentCode());
        }
        if (StringUtils.isNotBlank(customEquipmentUpdateRequestBean.getMac())) {
            lambdaUpdateWrapper.set(BloodSugarEquipment::getMac, customEquipmentUpdateRequestBean.getMac());
        }
        if (null!=customEquipmentUpdateRequestBean.getDepartmentId()) {
            lambdaUpdateWrapper.set(BloodSugarEquipment::getDepartmentId, customEquipmentUpdateRequestBean.getDepartmentId());
        }
        boolean update = bloodSugarEquipmentService.update(lambdaUpdateWrapper);
        if (update) {
            return Result.ok();
        } else {
            return Result.error().code(R_Code.R_UPDATE_FAIL);
        }

    }

    @ApiOperation(value = "pc添加设备信息")
    @PreAuthorize("hasAnyAuthority('sys:equipmentManage','sys:noAccess')")
    @PostMapping("addEquipmentInfo")
    public Result addEquipmentInfo(@Valid @RequestBody BloodSugarEquipment bloodSugarEquipment) {
        LambdaQueryWrapper<BloodSugarEquipment> equipmentLambdaQueryWrapper = Wrappers.lambdaQuery();
        equipmentLambdaQueryWrapper.eq(BloodSugarEquipment::getDepartmentId, bloodSugarEquipment.getDepartmentId())
                .eq(BloodSugarEquipment::getEquipmentCode, bloodSugarEquipment.getEquipmentCode());
        if (bloodSugarEquipmentService.list(equipmentLambdaQueryWrapper).size() > 0) {
            return Result.error().message("该科室下已经存在该仪器编号");
        }
        LambdaQueryWrapper<BloodSugarEquipment> equipmentLambdaQueryWrapper1 = Wrappers.lambdaQuery();
        equipmentLambdaQueryWrapper1.eq(BloodSugarEquipment::getMac, bloodSugarEquipment.getMac());
        if (bloodSugarEquipmentService.list(equipmentLambdaQueryWrapper1).size() > 0) {
            return Result.error().message("已存在该mac地址");
        }
        boolean save = bloodSugarEquipmentService.save(bloodSugarEquipment);
        if (save) {
            return Result.ok();
        } else {
            return Result.error().code(R_Code.R_ADD_FAIL);
        }
    }

    @ApiOperation(value = "pc删除设备信息")
    @PreAuthorize("hasAnyAuthority('sys:equipmentManage','sys:noAccess')")
    @GetMapping("deleteEquipmentInfo/{id}")
    public Result deleteEquipmentInfo(@ApiParam(name = "id", value = "仪器id") @NotNull(message = "仪器id不能为空") @PathVariable Integer id) {
        boolean b = bloodSugarEquipmentService.removeById(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.error().code(R_Code.R_DELETE_FAIL);
        }
    }

    @ApiOperation(value = "pc科室下的所有设备 下拉框使用")
    @PreAuthorize("hasAnyAuthority('sys:equipmentManage','sys:noAccess')")
    @GetMapping(value = {"getAllEquipmentInfo"})
    public Result getAllEquipmentInfo(@ApiParam(name = "departmentId", value = "科室id") @RequestParam(required = false) Integer departmentId) {
        LambdaQueryWrapper<BloodSugarEquipment> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BloodSugarEquipment::getDepartmentId,departmentId);
        List<BloodSugarEquipment> list = bloodSugarEquipmentService.list(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return Result.ok().data(list);
        } else {
            return Result.error().code(R_Code.R_NULL_DATA_FAIL);
        }
    }

    @ApiOperation(value = "pc查询所有条形码信息")
    @PreAuthorize("hasAnyAuthority('sys:equipmentManage','sys:noAccess')")
    @GetMapping(value = {"getAllBarcodeInfo"})
    public Result getAllBarcodeInfo(@ApiParam(name = "deanId", value = "院长id") @NotNull(message = "院长id不能为空") @RequestParam Integer deanId,
                                    @ApiParam(name = "departmentId", value = "科室id") @RequestParam(required = false) Integer departmentId,
                                    @ApiParam(name = "current", value = "当前页码") @RequestParam(required = false) Integer current,
                                    @ApiParam(name = "size", value = "一页显示多少条") @RequestParam(required = false) Integer size) {
        return bloodSugarPatientInfoService.getAllBarcodeInfo(deanId, departmentId, current, size);
    }

    @ApiOperation(value = "APP查询科室下所有的设备")
    @PostMapping("getAllEquipmentByDeanIdAndDepId")
    public Result getAllEquipmentByDeanIdAndDepId(String departmentId){
        if (org.apache.commons.lang.StringUtils.isEmpty(departmentId)) {
            return Result.error().code(301).message("科室Id不能为空");
        }
        QueryWrapper<BloodSugarEquipment> query = Wrappers.query();
        query.eq("department_id", departmentId);
        List<BloodSugarEquipment> list = bloodSugarEquipmentService.list(query);
        if (CollectionUtils.isNotEmpty(list)) {
            return Result.ok().data(list);
        } else {
            return Result.error().code(300).message("暂无数据");
        }
    }

}

